package com.example.broadcastdemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	private InterReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		receiver = new InterReceiver();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.wenhao.broadcast");
		
		this.registerReceiver(receiver, filter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/**
	 * 
	 * 点击按钮时发送广播
	 * 
	 * @param view
	 */
	public void sendBroadcast(View view){
		
		Intent intent = new Intent();
		intent.setAction("com.wenhao.broadcast");
		intent.putExtra("name", "wenhao");
		
		//this.sendBroadcast(intent);
		this.sendOrderedBroadcast(intent, null);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		if(receiver != null)
			this.unregisterReceiver(receiver);
	
	}
	
	class InterReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			
			Log.d("wenhao","inter Receiver..."+Thread.currentThread().getName());
			
			
		}
		
	}

}
